package org.jeecg.modules.demo.gmAdministrativeDivision.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.gmAdministrativeDivision.entity.GmAdministrativeDivision;
import org.jeecg.modules.demo.gmAdministrativeDivision.service.IGmAdministrativeDivisionService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 行政区划
 * @Author: jeecg-boot
 * @Date:   2025-07-11
 * @Version: V1.0
 */
@Api(tags="行政区划")
@RestController
@RequestMapping("/gmAdministrativeDivision/gmAdministrativeDivision")
@Slf4j
public class GmAdministrativeDivisionController extends JeecgController<GmAdministrativeDivision, IGmAdministrativeDivisionService> {
	@Autowired
	private IGmAdministrativeDivisionService gmAdministrativeDivisionService;
	
	/**
	 * 分页列表查询
	 *
	 * @param gmAdministrativeDivision
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "行政区划-分页列表查询")
	@ApiOperation(value="行政区划-分页列表查询", notes="行政区划-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<GmAdministrativeDivision>> queryPageList(GmAdministrativeDivision gmAdministrativeDivision,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
        QueryWrapper<GmAdministrativeDivision> queryWrapper = QueryGenerator.initQueryWrapper(gmAdministrativeDivision, req.getParameterMap());
		Page<GmAdministrativeDivision> page = new Page<GmAdministrativeDivision>(pageNo, pageSize);
		IPage<GmAdministrativeDivision> pageList = gmAdministrativeDivisionService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param gmAdministrativeDivision
	 * @return
	 */
	@AutoLog(value = "行政区划-添加")
	@ApiOperation(value="行政区划-添加", notes="行政区划-添加")
	@RequiresPermissions("gmAdministrativeDivision:gm_administrative_division:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody GmAdministrativeDivision gmAdministrativeDivision) {
		gmAdministrativeDivisionService.save(gmAdministrativeDivision);
		return Result.OK("添加成功！");
	}

	 /**
	  *   添加
	  *
	  * @param gmAdministrativeDivision
	  * @return
	  */
	 @AutoLog(value = "行政区划-添加")
	 @ApiOperation(value="行政区划-添加", notes="行政区划-添加")
	 @PostMapping(value = "/addNewVillage")
	 public Result<String> addNewVillage(@RequestBody GmAdministrativeDivision gmAdministrativeDivision) {
		 gmAdministrativeDivisionService.save(gmAdministrativeDivision);
		 return Result.OK("添加成功！");
	 }
	 /**
	  *   根据传入的市查对应下一级
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "行政区划-根据传入的市查对应下一级")
	 @ApiOperation(value="行政区划-根据传入的市查对应下一级", notes="行政区划-根据传入的市查对应下一级")
	 @PostMapping(value = "/selectByCity")
	 public Result<List<String>> selectByCity() {
		 String city = "赣州市";
		 if (StringUtils.isBlank(city)) {
			 return Result.error("参数'city'不能为空！");
			  }
		 LambdaQueryWrapper<GmAdministrativeDivision> queryWrapper = new LambdaQueryWrapper<>();
		 // 添加查询条件：county字段等于传入参数
		 queryWrapper.eq(GmAdministrativeDivision::getCity, city)
				 // 只查询town字段
				 .select(GmAdministrativeDivision::getCounty);
		 List<GmAdministrativeDivision> divisions = gmAdministrativeDivisionService.list(queryWrapper);

		 // 提取town字段并去重
		 List<String> county = divisions.stream()
				 .map(GmAdministrativeDivision::getCounty)
				 .filter(StringUtils::isNotBlank) // 过滤空值
				 .distinct() // 去重
				 .collect(Collectors.toList());

		 return Result.OK(county);
		 }


	 /**
	  *   根据传入的区或镇查对应下一级
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "行政区划-根据传入的区查对应下一级")
	 @ApiOperation(value="行政区划-根据传入的区查对应下一级", notes="行政区划-根据传入的区查对应下一级")
	 @PostMapping(value = "/selectByCounty")
	 public Result<List<String>> selectByCounty(@RequestBody JSONObject jsonObject) {
		 String county = jsonObject.getString("county");
		 if (StringUtils.isBlank(county)) {
			 return Result.error("参数'county'不能为空！");
		 }

		 LambdaQueryWrapper<GmAdministrativeDivision> queryWrapper = new LambdaQueryWrapper<>();
		 // 添加查询条件：county字段等于传入参数
		 queryWrapper.eq(GmAdministrativeDivision::getCounty, county)
				 // 只查询town字段
				 .select(GmAdministrativeDivision::getTown);

		 List<GmAdministrativeDivision> divisions = gmAdministrativeDivisionService.list(queryWrapper);

		 // 提取town字段并去重
		 List<String> towns = divisions.stream()
				 .map(GmAdministrativeDivision::getTown)
				 .filter(StringUtils::isNotBlank) // 过滤空值
				 .distinct() // 去重
				 .collect(Collectors.toList());

		 return Result.OK(towns);
	 }

	 /**
	  *   根据传入的区或镇查对应下一级
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "行政区划-根据传入的镇查对应下一级")
	 @ApiOperation(value="行政区划-根据传入的镇查对应下一级", notes="行政区划-根据传入的镇查对应下一级")
	 @PostMapping(value = "/selectByTown")
	 public Result<List<String>> selectByTown(@RequestBody JSONObject jsonObject) {
		 String town = jsonObject.getString("town");
		 String county = jsonObject.getString("county");
		 if (StringUtils.isBlank(town)) {
			 return Result.error("参数'town'不能为空！");
		 }

		 LambdaQueryWrapper<GmAdministrativeDivision> queryWrapper = new LambdaQueryWrapper<>();
		 // 添加查询条件：county字段等于传入参数
		 queryWrapper.eq(GmAdministrativeDivision::getCounty, county)
				 .eq(GmAdministrativeDivision::getTown, town)
				 // 只查询town字段
				 .select(GmAdministrativeDivision::getVillage);

		 List<GmAdministrativeDivision> divisions = gmAdministrativeDivisionService.list(queryWrapper);

		 // 提取town字段并去重
		 List<String> towns = divisions.stream()
				 .map(GmAdministrativeDivision::getVillage)
				 .filter(StringUtils::isNotBlank) // 过滤空值
				 .distinct() // 去重
				 .collect(Collectors.toList());

		 return Result.OK(towns);
	 }

	
	/**
	 *  编辑
	 *
	 * @param gmAdministrativeDivision
	 * @return
	 */
	@AutoLog(value = "行政区划-编辑")
	@ApiOperation(value="行政区划-编辑", notes="行政区划-编辑")
	@RequiresPermissions("gmAdministrativeDivision:gm_administrative_division:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody GmAdministrativeDivision gmAdministrativeDivision) {
		gmAdministrativeDivisionService.updateById(gmAdministrativeDivision);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "行政区划-通过id删除")
	@ApiOperation(value="行政区划-通过id删除", notes="行政区划-通过id删除")
	@RequiresPermissions("gmAdministrativeDivision:gm_administrative_division:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		gmAdministrativeDivisionService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "行政区划-批量删除")
	@ApiOperation(value="行政区划-批量删除", notes="行政区划-批量删除")
	@RequiresPermissions("gmAdministrativeDivision:gm_administrative_division:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.gmAdministrativeDivisionService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "行政区划-通过id查询")
	@ApiOperation(value="行政区划-通过id查询", notes="行政区划-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<GmAdministrativeDivision> queryById(@RequestParam(name="id",required=true) String id) {
		GmAdministrativeDivision gmAdministrativeDivision = gmAdministrativeDivisionService.getById(id);
		if(gmAdministrativeDivision==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(gmAdministrativeDivision);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param gmAdministrativeDivision
    */
    @RequiresPermissions("gmAdministrativeDivision:gm_administrative_division:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GmAdministrativeDivision gmAdministrativeDivision) {
        return super.exportXls(request, gmAdministrativeDivision, GmAdministrativeDivision.class, "行政区划");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("gmAdministrativeDivision:gm_administrative_division:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, GmAdministrativeDivision.class);
    }

}
